import { defaultsDeep } from "lodash";
import create from "zustand";

import { defaultAppConfig } from "./default";
import { AppConfig } from "./models";

export interface AppConfigActions {
  init: () => Promise<void>;
}

const setup = (config: AppConfig): void => {
  config.baseUrl = `${window.location.origin}${config.contextPath}`;
};

export const loadAppConfig = (): AppConfig => {
  const config = defaultsDeep(
    window.quiverAppConfig,
    defaultAppConfig
  ) as AppConfig;
  setup(config);
  return config;
};

export const appConfig = loadAppConfig();
export const init = async () => {
  console.log("app config init", JSON.stringify(appConfig));
  return Promise.resolve();
};

export const useAppConfig = create<AppConfig>()(() => loadAppConfig());
